package logic

import (
	"context"
	"github.com/pkg/errors"
	"mall/common/xerr"

	"mall/app/product/cmd/rpc/internal/svc"
	"mall/app/product/cmd/rpc/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetBrandListByPageLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetBrandListByPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetBrandListByPageLogic {
	return &GetBrandListByPageLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetBrandListByPage 分页获取品牌列表
func (l *GetBrandListByPageLogic) GetBrandListByPage(in *pb.GetBrandListByPageReq) (*pb.GetBrandListByPageResp, error) {
	list, err := l.svcCtx.BrandModel.FindBrandByPage(l.ctx, in.PageSize, in.Offset-1)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrorWithCode(xerr.DbError), "BrandModel.FindBrandByPage err: %v", err)
	}
	brandList := make([]*pb.Brand, len(list))
	for i, v := range list {
		brandList[i] = &pb.Brand{
			BrandId:     v.BrandId,
			Name:        v.Name,
			Descript:    v.Descript,
			FirstLetter: v.FirstLetter,
			Sort:        v.Sort,
		}
	}
	return &pb.GetBrandListByPageResp{BrandList: brandList}, nil
}
